﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Windows.Forms;
using HiStaff.Domain.Sys;
using HiStaff.Domain;
using HiStaff.Util;
using HiStaff.Control;
using Janus.Windows.GridEX;
using System.Data;
using HiStaff.iSystem;

namespace HiStaff.Base
{
    public partial class AddListData : FormBase
    {
        public string ListDataEdit { get; set; }
        DataTable dtColList = new DataTable();
        private int step = 0;
        private int allStep { get { return 8; } }
        private int percent { get { return (step * 100) / allStep; } }
        public AddListData()
        {
            InitializeComponent();
        }
        protected override bool FormLoad()
        {
            base.CreateCommandBar(CommandBarManager.ActionCommand.Save,
                CommandBarManager.ActionCommand.Delete,
                CommandBarManager.ActionCommand.Separator,
                CommandBarManager.ActionCommand.Exit);

            List<SY_DATA_TYPE> lstDataType = Service.SystemClient.GetDataType();
            List<SY_COMPARE_TYPE> lstCompareType = Service.SystemClient.GetCompareType();
            List<SY_TAB_LIST> _lstListData = Service.SystemClient.GetAllDataList();
            var lstListData = (from obj in _lstListData
                               where (string.IsNullOrEmpty(ListDataEdit) || obj.LIST_NAME != ListDataEdit)
                               select obj).ToList();
            lstListData.Insert(0, new SY_TAB_LIST());
            grdCol.RootTable.Columns["DATA_TYPE"].ValueList.PopulateValueList(lstDataType, "ID", "DESCRIPTION");
            grdCol.RootTable.Columns["COMPARE_TYPE1"].ValueList.PopulateValueList(lstCompareType, "ID", "NAME");
            grdCol.RootTable.Columns["COMPARE_TYPE2"].ValueList.PopulateValueList(lstCompareType, "ID", "NAME");
            grdCol.RootTable.Columns["COMPARE_TYPE3"].ValueList.PopulateValueList(lstCompareType, "ID", "NAME");
            grdCol.RootTable.Columns["LIST_DATA"].ValueList.PopulateValueList(lstListData, "TABLE_NAME", "LIST_NAME");
            List<SY_COLUMNS_LIST> colList;
            if (string.IsNullOrEmpty(ListDataEdit))
            {
                colList = new List<SY_COLUMNS_LIST>();
                colList.Add(new SY_COLUMNS_LIST
                {
                    COLUMN_NAME = Constant.COL_ID,
                    DESCRIPTION = "KEY",
                    DATA_TYPE = 1,
                    REQUIREMENT = 1,
                    STATUS = 1,
                    ORDERING = 0
                });
                colList.Add(new SY_COLUMNS_LIST
                {
                    COLUMN_NAME = Constant.COL_NAME,
                    DESCRIPTION = "TÊN DANH MỤC",
                    DATA_TYPE = 2,
                    DATA_LENGTH = 300,
                    REQUIREMENT = 1,
                    STATUS = 1,
                    ORDERING = 1
                });
                colList.Add(new SY_COLUMNS_LIST
                {
                    COLUMN_NAME = Constant.COL_STATUS,
                    DESCRIPTION = "SỬ DỤNG",
                    DATA_TYPE = 4,
                    REQUIREMENT = 1,
                    STATUS = 1,
                    ORDERING = 99
                });
            }
            else
            {
                SY_TAB_LIST objTab = Data.LISTTABLE.Find(obj => obj.TABLE_NAME == ListDataEdit);
                txtTableName.Text = objTab.TABLE_NAME;
                txtListDataName.Text = objTab.LIST_NAME;
                chkStatus.Checked = objTab.STATUS == 1;
                chkUserEdit.Checked = objTab.LIST_TYPE == 1;

                colList = Service.SystemClient.GetColumnList(ListDataEdit);
            }
            dtColList = ConvertHelper.ListToDataTable(colList);
            grdCol.DataSource = dtColList;
            grdCol.Refresh();
            return true;
        }
        protected override void SetToolStripButtonBar(CommandBarManager.ActionCommand cmd)
        {
        }
        protected override bool SaveButtonClick()
        {
            ExcuteAsynchronous();
            return true;
        }
        protected override bool DeleteButtonClick()
        {
            if (grdCol.SelectedItems.Count == 0) return false;
            GridEXSelectedItem item = grdCol.SelectedItems[0];
            if (item.RowType == RowType.Record)
            {
                DataRow dr = ((DataRowView)item.GetRow().DataRow).Row;
                ((DataTable)grdCol.DataSource).Rows.Remove(dr);
                grdCol.Refresh();
            }
            return true;
        }
        protected override bool AsynchronousFunctionWithBGW(System.ComponentModel.BackgroundWorker bgWorker)
        {
            SY_TAB_LIST objTab;
            // Save table
            step = 1;
            objTab = new SY_TAB_LIST
            {
                TABLE_NAME = txtTableName.Text.ToUpper(),
                LIST_NAME = txtListDataName.Text,
                STATUS = chkStatus.Checked ? 1m : 0m,
                LIST_TYPE = chkUserEdit.Checked ? 1m : 0m
            };
            Service.SystemClient.SaveTableList(objTab);
            bgWorker.ReportProgress(percent, "Đang thực hiện");
            // Save columns list
            Service.SystemClient.SaveColumnsList(dtColList);
            bgWorker.ReportProgress(percent, "Đang thực hiện");
            // Create table
            step++;
            Service.SystemClient.CreateTableDataList(txtTableName.Text);
            bgWorker.ReportProgress(percent, "Đang thực hiện");
            // Create procedure store
            step++;
            var tableFullName = Constant.PREFIX_TABLE_LIST + txtTableName.Text.ToUpper();
            Service.SystemClient.CreateUpdateStore(tableFullName);
            bgWorker.ReportProgress(percent, "Đang thực hiện");
            step++;
            Service.SystemClient.CreateDeleteStore(tableFullName);
            bgWorker.ReportProgress(percent, "Đang thực hiện");
            step++;
            Service.SystemClient.CreateSelectStore(tableFullName);
            bgWorker.ReportProgress(percent, "Đang thực hiện");
            // Done
            bgWorker.ReportProgress(100, "Hoàn tất");
            return true;
        }
    }
}
